package com.ruoyi.system.controller.bm;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.enums.BMAreaEnum;
import com.ruoyi.common.core.enums.BMIndexEnum;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.domain.BaseContest;
import com.ruoyi.system.domain.bm.BMCoach;
import com.ruoyi.system.domain.bm.BMPack;
import com.ruoyi.system.domain.bm.BMPlayer;
import com.ruoyi.system.domain.bm.BMTeam;
import com.ruoyi.system.domain.bm.dto.GetOrderStatusDTO;
import com.ruoyi.system.service.IBaseBMPackService;
import com.ruoyi.system.service.IBaseContestService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联赛
 */
@ApiOperation("联赛")
@RestController
@RequestMapping("/bm/contest")
public class BMContestController extends BaseController {

    @Autowired
    private IBaseContestService baseContestService;

    @Autowired
    private IBaseBMPackService baseBMPackService;

    /**
     * 查询联赛列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Integer indexId, String areaName) {
        List<Long> value = BMIndexEnum.getValue(indexId);
        if (CollUtil.isEmpty(value)) {
            return new TableDataInfo();
        }
        startPage();
        LambdaQueryWrapper<BaseContest> wrapper = Wrappers.<BaseContest>lambdaQuery()
                .in(BaseContest::getId, value)
                .like(StringUtils.isNotBlank(areaName), BaseContest::getContestName, areaName);
        return getDataTable(baseContestService.list(wrapper));
    }

    /**
     * 查询联赛-详细信息
     */
    @GetMapping("/detail")
    public AjaxResult detail(Long contestId) {
        return success(baseContestService.getById(contestId));
    }

    /**
     * 创建球队
     */
    @PostMapping("/createTeam")
    public AjaxResult createTeam(@RequestBody BMTeam team) {
        return success(baseContestService.createTeam(team));
    }

    /**
     * 编辑球队
     */
    @PostMapping("/editTeam")
    public AjaxResult editTeam(@RequestBody BMTeam team) {
        return success(baseContestService.editTeam(team));
    }

    /**
     * 查询球队详细信息
     */
    @GetMapping("/teamDetail")
    public AjaxResult teamDetail(Long teamId) {
        return success(baseContestService.teamDetail(teamId));
    }

    /**
     * 获取我的球队列表
     */
    @GetMapping("/myTeam")
    public AjaxResult myTeam(Integer indexId) {
        return success(baseContestService.myTeam(indexId));
    }

    /**
     * 获取教练列表
     */
    @GetMapping("/coachList")
    public AjaxResult coachList(Long teamId) {
        return success(baseContestService.coachList(teamId));
    }

    /**
     * 获取球员列表
     */
    @GetMapping("/playerList")
    public AjaxResult playerList(Long teamId) {
        return success(baseContestService.playerList(teamId));
    }

    /**
     * 创建教练
     */
    @PostMapping("/createCoach")
    public AjaxResult createCoach(@RequestBody BMCoach coach) {
        return success(baseContestService.createCoach(coach));
    }

    /**
     * 创建球员
     */
    @PostMapping("/createPlayer")
    public AjaxResult createPlayer(@RequestBody BMPlayer player) {
        return success(baseContestService.createPlayer(player));
    }

    /**
     * 删除球员
     */
    @PostMapping("/deletePlayer")
    public AjaxResult deletePlayer(@RequestParam Long id) {
        return success(baseContestService.deletePlayer(id));
    }

    /**
     * 删除教练
     */
    @PostMapping("/deleteCoach")
    public AjaxResult deleteCoach(@RequestParam Long id) {
        return success(baseContestService.deleteCoach(id));
    }

    /**
     * 获取订单状态
     */
    @GetMapping("/getOrderStatus")
    public AjaxResult getOrderStatus(@RequestBody @Validated GetOrderStatusDTO param) {
        return success(baseContestService.getOrderStatus(param));
    }

    /**
     * 获取礼包列表
     */
    @GetMapping("/giftList")
    public TableDataInfo giftList(@RequestParam Long contestId) {
//        List<BaseContest> baseContests = baseContestService.selectBaseContestList(new BaseContest());
//        if (CollUtil.isEmpty(baseContests)){
//            return getDataTable(new ArrayList<>());
//        }
//        dto.setContestIds(CommonStreamUtil.transList(baseContests, BaseContest::getId));
        BMPack dto = new BMPack();
        dto.setContestId(contestId);
        startPage();
        List<BMPack> list = baseBMPackService.selectList(dto);
        return getDataTable(list);
    }

    /**
     * 获取省份下拉框
     */
    @GetMapping("/provinceList")
    public AjaxResult provinceList() {
        return success(baseContestService.provinceList());
    }

    /**
     * 获取赛区下拉框
     */
    @GetMapping("/areaList")
    public AjaxResult areaList(Integer indexId) {
        List<BMAreaEnum> value1 = BMAreaEnum.getValue(indexId);
        List<Map<String, Object>> list = new ArrayList<>();
        for (BMAreaEnum value : value1) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", value.getCode());
            map.put("value", value.getInfo());
            list.add(map);
        }
        return success(list);
    }

    /**
     * 球员 球队总和付款
     */
    @PostMapping("/totalPlayerPay")
    public AjaxResult totalPlayerPay(@RequestBody BMTeam team) {
        return success(baseContestService.totalPlayerPay(team));
    }
}
